package com.ke.forum.socket.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ke.forum.common.common.Result;
import com.ke.forum.mbg.dao.ChatSessionMapper;
import com.ke.forum.mbg.entity.ChatHistory;
import com.ke.forum.mbg.dao.ChatHistoryMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ke.forum.mbg.entity.ChatSession;
import com.ke.forum.mbg.entity.dto.ChatHistoryDto;
import com.ke.forum.mbg.entity.dto.UserDto;
import com.ke.forum.socket.service.ChatHistoryService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 珂
 * @since 2021-07-02
 */
@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements ChatHistoryService {

    @Autowired
    ChatHistoryMapper chatHistoryMapper;
    @Autowired
    ChatSessionMapper chatSessionMapper;

    @Override
    public Result listChatHistoryBySessionId(Long sessionId, Integer current) {
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        Page<ChatHistoryDto> page = new Page<>();
        page.setCurrent(current);
        page.setSize(20);
        ChatSession chatSession = chatSessionMapper.selectById(sessionId);
        if (!chatSession.getFromUuid().equals(userDto.getUuid())){
            return Result.FAIL("无法获取他人的聊天记录");
        }
        Page<ChatHistoryDto> chatHistoryDtoPage = chatHistoryMapper.listChatHistoryBySessionId(sessionId, page);
        return Result.SUCCESS(chatHistoryDtoPage);
    }
}
